<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2020/11/11 16:58
 */

namespace app\api\exception;

use app\common\lib\Show;
use think\exception\Handle;
use think\Response;
use Throwable;

class Http extends Handle {

    public $httpStatus = 500;

    /**
     * Render an exception into an HTTP response.
     *
     * @access public
     * @param \think\Request   $request
     * @param Throwable $e
     * @return Response
     */
    public function render($request, Throwable $e): Response {
        if ($e instanceof \think\Exception) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        if ($e instanceof \think\exception\HttpResponseException) {
            return parent::render($request, $e);
        }

        if (method_exists($e, "getStatusCode")) {
            $httpStatus = $e->getStatusCode();
        }else{
            $httpStatus = $this->httpStatus;
        }
        // 添加自定义异常处理机制
        return Show::error([], $e->getMessage(), 0, $httpStatus);
    }

}